package com.example.demo.api;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.bean.currency.Sucess;
import com.example.demo.bean.dep.Dep;
import com.example.demo.bean.user.User;
import com.example.demo.mapper.dep.DepMapper;
import com.example.demo.mapper.user.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/api")
@RestController
@SpringBootApplication
public class SampleDep {
    @Autowired
    private DepMapper depmapper;
    @Autowired
    private UserMapper userMapper;

    //新增编辑部门
    @PostMapping("addDep")
    public Sucess addDep(@RequestBody Dep dep) {

        Sucess<Long> sucess = new Sucess();
        System.out.print(dep.toString());
        if (depmapper.selectById(dep.getId()) == null) {
            depmapper.insert(dep);
            sucess.setData(dep.getId());
        } else {
            depmapper.updateById(dep);
            sucess.setData(dep.getId());

        }
        return sucess;
    }


    //    关联部门
    @PostMapping("/glDep")
    public Sucess glDep(@RequestBody User user) {
        Sucess sucess = new Sucess();
        System.out.println(user.getDepId());
        if (depmapper.selectById(user.getDepId()) == null) {
            sucess.setCode(403);
            sucess.setMessage("部门不存在");
            return sucess;
        }
        if (userMapper.selectById(user.getId()) == null) {
            sucess.setCode(403);
            sucess.setMessage("用户不存在");
            return sucess;
        }
        // 查询出完整的用户对象
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", user.getId()).set("dep_id", user.getDepId());
        userMapper.update(null, updateWrapper);

        return sucess;

    }


}
